package com.itheima.controller;


import com.itheima.health.entity.Result;
import com.itheima.health.service.OrderSettingService;
import com.itheima.health.utils.date.DateUtils;
import com.itheima.health.utils.poi.POIUtils;
import io.swagger.annotations.Api;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

@RestController
@Api(tags = "预约管理开发")  // 作用在类上
@RequestMapping("ordersetting")
public class OrderSettingController {

    @Reference
    private OrderSettingService orderSettingService;

    //判断用户请求时间是否为以往日期  zh
    @GetMapping("judgeIsOldDay/{dateparam}")
    public Result judgeIsOldDay(@PathVariable("dateparam") String dateparam){
        Date today = DateUtils.getToday();
        long todayTime = today.getTime();
        Date date = DateUtils.parseString2Date(dateparam);
        long thisTime = date.getTime();
        return new Result(thisTime < todayTime);
    }


    //修改数据库中的最大预约人数
    @PutMapping("updateOrdersettingNumberBySettingData/{number}/{settingDay}")
    public Result updateOrdersettingNumberBySettingData(@PathVariable("number") int number,@PathVariable("settingDay") String settingDay) {
        return new Result(orderSettingService.updateOrdersettingNumberBySettingData(number, settingDay));
    }


    //根据前端传入的年和月查询对应的orderSetting
    @GetMapping("findSettingData/{year}/{month}")
    public Result findSettingData(@PathVariable("year") int year,@PathVariable("month") int month) {
        return new Result(orderSettingService.findSettingData(year, month));
    }


    @PostMapping("importOrderSettings")
    public Result importOrderSettings(@RequestParam("excelFile") MultipartFile file) {

        //使用工具类将一个xlsx文件转换为一个存放String数组的list集合
        List<String[]> orderSettingList = POIUtils.readExcel(file);

        //调用方法将集合放入数据库中
        orderSettingService.importOrderSettings(orderSettingList);

        //返回结果
        return new Result(true);
    }


}








